package com;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBCon {
	Connection con = null;
	Statement st = null;
	ResultSet rs = null;
	String driver=null;
	String url = null;
	String username = null;
	String password = null;

	public static Connection dbCon() throws SQLException, ClassNotFoundException {
		Class.forName("org.mariadb.jdbc.Driver");
		String url = "jdbc:mariadb://localhost:3305/epidemicmanagement";
		Connection conn = DriverManager.getConnection(url,"root","888888");
		Statement stmt = conn.createStatement();
		return conn;
	}

	/*
	 * 增删改
	 */
	public  int query(String sql) throws ClassNotFoundException,SQLException {
		int rs = 0;
		con = dbCon();
		try {
			st = con.createStatement();
			rs = st.executeUpdate(sql);
		} catch (SQLException e) {
			close();
			e.printStackTrace();
		}

		return rs;
	}

	/*
	 * 查
	 */
	public ResultSet find(String sql) throws ClassNotFoundException,SQLException {

		try {
			con = dbCon();
			st = con.createStatement();
			rs = st.executeQuery(sql);
		} catch (SQLException e) {
			close();
			e.printStackTrace();
		}
		return rs;
	}

	/*
	 * 关闭数据库
	 */
	public void close() throws ClassNotFoundException,SQLException {
		try {
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (con != null)
				con.close();
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}
}
